<!--打印文件-->
<template>
    <div>
        <Modal :value="value" @input="handleInput" :title="title">
            <div style="font-size:14px;color:#383A4A;margin-bottom:8px;">
                <span style="color:#F45050">*</span>选择打印文件类型
            </div>
            <CheckboxGroup v-model="selected">
                <Checkbox
                    v-for="item in options"
                    :key="item.value"
                    :label="item.value"
                >{{item.label}}</Checkbox>
            </CheckboxGroup>
            <div slot="footer">
                <Button type="primary" :loading="viewLoading" @click="previewHandle">打印</Button>
                <Button type="default" @click="printFileCancel">取消</Button>
            </div>
        </Modal>
    </div>
</template>
<script>
const pdfUrl = '/static/pdf/web/viewer.html?file='
export default {
    props: {
        value: {
            type: Boolean,
            default: false
        },
        title: {
            type: String,
            default: ''
        },
        options: {
            type: Array,
            default: () => []
        },
        /**
         * 查询打印 预览 url的方法
         * @param {String} v 选择的文件类型
         * @returns {Promise} 返回一个Promise
         */
        queryFileFn: {
            type: Function
        }
    },
    watch: {
        'options.length' (v) {
            this.handleOptionsChange(v)
        }
    },
    data () {
        return {
            selected: [],
            printFileTypes: [],
            iframeList: [],
            viewLoading: false,
            downloading: false
        }
    },
    created () {
        this.handleOptionsChange()
    },
    methods: {
        // 文件类型选项改变
        handleOptionsChange () {
            if (this.options.length > 0) {
                let selected = []
                this.options.forEach(item => {
                    if (this.selected.indexOf(item.value) >= 0 || item.checked) {
                        selected.push(item.value)
                    }
                })
                this.selected = selected
            } else {
                this.selected = []
            }
        },
        handleInput (v) {
            if (v === false) {
                this.downloading = false
                this.viewLoading = false
                this.iframeList = []
                this._timer && clearTimeout(this._timer)
            }
            this.$emit('input', v)
        },
        // 查询pdf文件
        queryPDF (cb) {
            this.queryFileFn(this.selected.join(',')).then(res => {
                this.handleInput(false)
                if (res.data.code === '0000') {
                    const data = res.data.data || []
                    if (data.length <= 0) {
                        this.$Message.warning('没有找到相关的文件')
                    }
                    cb && cb(data)
                } else {
                    this.$Message.error({
                        content: res.data.msg,
                        duration: 4
                    })
                    this.viewLoading = false
                    this.downloading = false
                }
            }).catch(err => {
                this.viewLoading = false
                this.downloading = false
                this.$Message.warning(`请求异常：${err}`)
            })
        },
        // 打印前校验
        befortPrint () {
            const flag = this.selected.length <= 0
            if (flag) {
                this.$Message.warning('请输选择文件类型')
            }
            return !flag
        },
        // 预览
        previewHandle () {
            if (this.befortPrint() && this.queryFileFn) {
                this.viewLoading = true
                this.queryPDF((data) => {
                    data.forEach(item => {
                        window.open(pdfUrl + item)
                    })
                    this.viewLoading = false
                })
            }
        },
        // 取消
        printFileCancel () {
            this.handleInput(false)
        }
    }
}
</script>
